Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

136
Vistas
How can I make it so that at any point during my function the user can type "reset" and it will take them back to the beginning of my function?
let level = 0;
let usersName;
let path;

const getBotReply = (msg) => {

    if (level === 0) {
        level = 1;
        usersName = msg;
        return "Chur " + usersName + ". Do you live in Raglan?";
    }

    if (level === 1) {
        level = 2;
        if (msg === "yes") {
        path = "left-yes";
        return "Do you know how to surf?";
    }

        if (msg === "no") {
        path = "right-no";
        return "Are you from Auckland?";
    }
}

Basically I want it so that instead of typing yes or no. The user will type reset and it will return to "level 0"

I've tried:

    if (msg === "reset") {
    level = 0;
}
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

in this case, I believe it would be preferable to include a while loop.

while(msg === "reset"){level = 0};
about 4 years ago · Juan Pablo Isaza Denunciar

0

If you put your if-check for reset at the top then you have done it correctly, and you simply forgot a return in the solution you suggested?

If you don't include a return stament in the if-check the function will just continue, so it will run the next if check, which matches the level being 0 and it will assume that "reset" is the name of the user.

So if you include

if(msg === "reset"{
 level = 0
 return
}

It should work just fine, you can leave the return blank or include a message. I included a JS fiddle which is literally your code with the fixed if check: https://jsfiddle.net/5tu7c20n/

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda